// import * as math from "mathjs"

//跳转路由
export function routerPush(url: string, param?: object) {
    uni.navigateTo({ url: url + formatUrlParam(param) })
}

//路由回退
export function routerBack(delta?: number) {
    uni.navigateBack({
        delta: delta && 1
    })
}

//将对象转换成query string（GET请求需要）
export function formatUrlParam(param: object) {
    let paramsStr = ''
    if (param) {
        for (let key in param) {
            if (paramsStr == '') {
                paramsStr = `?${key}=${param[key]}`
            } else {
                paramsStr += `&${key}=${param[key]}`
            }
        }
    }
    return paramsStr
}

//显示toast提示
export function toast(msg: string) {
    uni.showToast({
        title: msg,
        icon: 'none',
        duration: 2500
    })
}

//判断是否是空数据
export function isEmpty(value: any) {
    return value === undefined || value === null || (typeof value === 'object' && Object.keys(value).length === 0) || (typeof value === 'string' && value.trim().length === 0)
}
//去除字符串空格
export function replaceStr(str: string) {
    return str.replace(/\s+/g, '')
}

//判断是否是正整数
export function isInt(num: string) {
    if (!/(^[1-9]\d*$)/.test(num)) {
        return false
    } else {
        return true
    }
}

// //加
// export function mathAdd(num1: number = 0, num2: number = 0) {
// 	return Number(math.add(math.bignumber(num1), math.bignumber(num2)))
// }
// // 减
// export function mathSubtract(num1: number = 0, num2: number = 0) {
// 	return Number(math.subtract(math.bignumber(num1), math.bignumber(num2)))
// }
// // 乘
// export function mathMultiply(num1: number = 0, num2: number = 0) {
// 	return Number(math.multiply(math.bignumber(num1), math.bignumber(num2)))
// }
// // 除
// export function mathDivide(num1: number = 0, num2: number = 0) {
// 	return Number(math.divide(math.bignumber(num1), math.bignumber(num2)))
// }

//是否是手机号
export function isPhone(phone: string) {
    const reg = /^1[2-9]\d{9}$/
    return reg.test(phone)
}

//是否是链接地址
export function isUrl(url: string) {
    const reg = /(http|https):\/\/([\w.]+\/?)\S*/
    return reg.test(url)
}

/*** 移动端表单数据映射 ***/
//获取到详情映射回组件参数,多个参数将检索到的参数拼接成数组形式赋值
export function mapFormData(formData: any, resultData: any) {
    formData.forEach((item: any, index: number) => {
        let name = item.name.split(',')
        if (name.length > 1) {
            let valueArr = <any>[]
            name.forEach((nameItem: any, index: number) => {
                valueArr[index] = resultData[nameItem]
            })
            //判断多个字段数组回显值是否为空
            let currentArrIsEmpty = false
            valueArr.map((item: any) => {
                isEmpty(item) ? (currentArrIsEmpty = true) : ''
            })
            if (!currentArrIsEmpty) formData[index].values = valueArr
        } else {
            formData[index].values = resultData[name]
        }
    })
}
export function mapFormDataObject(formData: any, resultData: any) {
    for (const keys in formData) {
        let key = keys.split(',')
        if (key.length > 1) {
            let valueArr = <any>[]
            key.forEach((item: any, index: number) => {
                valueArr[index] = resultData[item]
            })
            formData[keys] = valueArr
        } else {
            formData[keys] = resultData[key[0]]
        }
    }
}

export function getUUID() {
    // 获取唯一值
    return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let r = (Math.random() * 16) | 0,
            v = c === 'x' ? r : (r & 0x3) | 0x8
        return v.toString(16)
    })
}

//获取文件后缀名
export function getFileSuffix(fileName: string) {
    return fileName.substring(fileName.lastIndexOf('.') + 1)
}

//复制文本到剪切板
export function copyText(value: string) {
    uni.setClipboardData({
        data: value,
        success: function () {
            toast('复制成功')
        }
    })
}

//预览图片
export function previewImage(url: string, current?: number) {
    uni.previewImage({
        urls: [url],
        current: current
    })
}

//对比版本号
export function compareVersion(v1: any, v2: any) {
    //v1：需要对比的版本号； v2：用于对比的参照版本号； return 1：对比数比参照数大 -1：对比数比参照数小 0：对比数等于参照数
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)
    while (v1.length < len) {
        v1.push('0')
    }
    while (v2.length < len) {
        v2.push('0')
    }
    for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i])
        const num2 = parseInt(v2[i])
        if (num1 > num2) {
            return 1
        } else if (num1 < num2) {
            return -1
        }
    }
    return 0
}

// // 导出AMapUI 防止报红
// export function getAMapUI() {
// 	return new Promise<any>((resolve, reject) => {
// 		if (window.AMapUI) {
// 			resolve(window.AMapUI)
// 		}
// 	})
// }

// 导出AMapUI 防止报红
export function getCloudImg(src) {
    return 'https://aiouwei.oss-cn-hangzhou.aliyuncs.com/citywall/xiaochengxu/img/' + src
}
